﻿package org.nickro.project.utils {

    public class Maths {

        public static const RIGHT_ANGLE:Number = (Math.PI / 2);
        public static const DEG_TO_RAD:Number = (Math.PI / 180);
        public static const RAD_TO_DEG:Number = (180 / Math.PI);

        public function Maths(){
            throw (new Error("ERROR org.nickro.project.utils.Maths constructor: Class never intended to be instantiated"));
        }
		/**
		 * 向量转换为角度
		 * @param	dx
		 * @param	dy
		 * @return
		 */
        public static function vectorToDegrees(dx:Number, dy:Number):Number{
            var angle:Number;
            if (dx == 0){
                angle = ((dy)<=0) ? -90 : 90;
            } else {
                angle = (Math.atan((dy / dx)) * RAD_TO_DEG);
                if (dx < 0){
                    angle = (angle + ((dy)<0) ? -180 : 180);
                };
            };
            return (angle);
        }
		/**
		 * 向量转换为弧度
		 * @param	dx
		 * @param	dy
		 * @return
		 */
		public static function vectorToRadians(dx:Number, dy:Number):Number{
            var angle:Number;
            if (dx == 0){
                angle = ((dy)<=0) ? -(RIGHT_ANGLE) : RIGHT_ANGLE;
            } else {
                angle = Math.atan((dy / dx));
                if (dx < 0){
                    angle = (angle + ((dy)<0) ? -(Math.PI) : Math.PI);
                };
            };
            return (angle);
        }

		/**
		 * 返回一个[min,max]之间的随机整数
		 * @param	Min		
		 * @param	Max
		 * @return
		 */
        public static function randomInt(Min:int, Max:int):Number{
            var tmp:int;
            if (Min > Max){
                tmp = Min;
                Min = Max;
                Max = tmp;
            };
            var range:int = ((Max - Min) + 1);
            return ((Min + Math.floor((Math.random() * range))));
        }
		
		/**
		 * 返回一个[min,max]之间的随机浮点数
		 * @param	Min
		 * @param	Max
		 * @return
		 */
        public static function randomFloat(Min:Number, Max:Number):Number{
            var tmp:Number;
            if (Min > Max){
                tmp = Min;
                Min = Max;
                Max = tmp;
            };
            return ((Min + (Math.random() * (Max - Min))));
        }
    }
}